package co.fitstart.weapp.api.service.impl;

import java.util.Arrays;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;

import co.fitstart.entity.order.OrderStatus;
import co.fitstart.entity.order.OrderType;
import co.fitstart.entity.order.PaymentStatus;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.User;
import co.fitstart.entity.utils.DateFormatUtils;
import co.fitstart.entity.utils.DateUtils;
import co.fitstart.weapp.api.dao.OrderDao;
import co.fitstart.weapp.api.dao.StudentDao;
import co.fitstart.weapp.api.service.StudentService;

/**
 * 
 * @author dan
 *
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService{
    
    @Dao
    private StudentDao studentDao;
    
    @Dao
    private OrderDao orderDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.StudentService#load(co.fitstart.entity.user.User)
     */
    public Student load(User user) {
        return studentDao.load(user);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.StudentService#isVip(co.fitstart.entity.user.Student)
     */
    public boolean isVip(Student student) {
        Long sum = orderDao.sumOfflinePaid(Arrays.asList(new OrderStatus[]{OrderStatus.PENDING, OrderStatus.PROCESSING, OrderStatus.COMPLETE}), Arrays.asList(new OrderType[]{OrderType.INDIVIDUAL, OrderType.YEAR}), PaymentStatus.PAID, DateUtils.parseDate(DateFormatUtils.format(new Date(), "yyyy") + "-01-01 00:00:00", "yyyy-MM-dd HH:mm:ss"), student);
        
        if(sum != null && sum >= 6000000l) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.StudentService#load(java.lang.Long)
     */
    public Student load(Long studentId) {
        Student student = genericDaoSupport.load(Student.class, studentId);
        return (student == null || student.isDisabled()) ? null : student;
    }

}
